package com.great.chatroom.springbootnettywebsocketdemo.web.websocket;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 *  <div  style="border:2px solid none;margin:20px;padding:15px;font-size:24px;text-align:center;color:aqua;">
 *
 *
 *
 *  </div>
 * @author v_zhangshun01
 * @date 2021/12/13 7:35 PM
 */
@Component
public class WebSocketChildChannelHandler extends ChannelInitializer<SocketChannel> {

	@Resource(name = "webSocketServerHandler")
	private ChannelHandler webSocketServerHandler;

	@Resource(name = "httpRequestHandler")
	private ChannelHandler httpRequestHandler;

	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		// HTTP编码解码器
		ch.pipeline().addLast("http-codec", new HttpServerCodec());
		// 把HTTP头、HTTP体拼成完整的HTTP请求
		ch.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));
		// 方便大文件传输，不过实质上都是短的文本数据
		ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());
		ch.pipeline().addLast("http-handler", httpRequestHandler);
		ch.pipeline().addLast("websocket-handler", webSocketServerHandler);
	}

}
